package com.egoo.ticket.server.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.EwsUserDao;
import com.egoo.ticket.server.dao.WorkOrderDao;
import com.egoo.ticket.server.dao.WorkOrderTypeDao;
import com.egoo.ticket.server.pojo.dto.PageOutputDto;
import com.egoo.ticket.server.pojo.dto.SelectByNameAndTypeDto;
import com.egoo.ticket.server.pojo.dto.WorkOrderTypeInputDto;
import com.egoo.ticket.server.pojo.entity.FormTemplate;
import com.egoo.ticket.server.pojo.vo.InteractiveDataVo;
import com.egoo.ticket.server.pojo.vo.WorkOrderTypeVo;
import com.egoo.ticket.server.service.FormTemplateService;
import com.egoo.ticket.server.service.WorkOrderTypeService;
import com.egoo.ticket.server.utils.common.CommonUtil;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 工单类型的接口
 *
 * @author Donny_dong
 */
@RestController
@RequestMapping(value = "/v1/workOrderType", produces = "application/json")
@Api(value = "工单类型管理",tags = "工单类型管理",description = "工单类型管理")
@Slf4j
public class WorkOrderTypeController {

	@Autowired
	private WorkOrderTypeService workOrderTypeService;
	@Autowired
	private EwsUserDao ewsUserDao;
	@Autowired
	private WorkOrderDao workOrderDao;
	@Autowired
	private FormTemplateService formTemplateService;
	@Autowired
	private WorkOrderTypeDao workOrderTypeDao;


	@ApiOperation(value = "获取时间戳", notes = "获取时间戳")
	@RequestMapping(method = RequestMethod.GET, value = "/getTimestamp")
	public DbResponse getTimestamp() {
		DbResponse dbResponse = new DbResponse();
		dbResponse.setRetCode(200);
		dbResponse.setData(System.currentTimeMillis());
		return dbResponse;
	}

	@ApiOperation(value = "获取工单类型列表", notes = "获取工单类型列表")
	@RequestMapping(method = RequestMethod.POST, value = "/getWorkOrderTypeList")
	public DbResponse getWorkOrderTypeList(@ApiParam(value = "workOrderTypeVo",
			required = true) @RequestBody WorkOrderTypeVo workOrderTypeVo,
										   HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		try {
			HashMap<String, Object> searchMap = new HashMap<>(2);
			searchMap.put("tenantId", tenantId);
			List<WorkOrderTypeVo> workOrderTypeVos = workOrderTypeService.getWorkOrderTypeListByCondition(searchMap);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, ResponseConstant.SELECT_SUCCESS_MSG, workOrderTypeVos);
		} catch (Exception e) {
			log.error("获取工单类型列表异常：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"获取工单类型列表异常：" + e);
		}
	}


	@ApiOperation(value = "获取所有的表单模板", notes = "获取所有的表单模板")
	@RequestMapping(method = RequestMethod.GET, value = "/selectAllFormTemplate")
	public DbResponse selectAll(HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		List<FormTemplate> formTemplateList = formTemplateService.selectAllFormTemplateIdAndName(tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(formTemplateList);
		return dbResponse;
	}

	@ApiOperation(value = "新建工单类型", notes = "新建工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/create")
	public DbResponse create(@ApiParam(value = "create", required = true) @RequestBody JSONObject jsonObject,
							 HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		String userId = jsonObject.getString("userId");
		if (StringUtils.isEmpty(userId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "请选择用户");
		}
		//是否有未编辑和未删除但名字已存在的
		JSONObject workordertypeformInfo = jsonObject.getJSONObject("workordertypeformInfo");
		JSONObject newworkordernodeInfo = jsonObject.getJSONObject("newworkordernodeInfo");
		JSONArray workordernodeInfo = jsonObject.getJSONArray("workordernodeInfo");

		String workOrderTypeName = workordertypeformInfo.getString("workordertypeName");
		if (CommonUtil.isSpecialChar(workOrderTypeName)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "流程名字不能出现特殊字符");
		}
		if (null != newworkordernodeInfo && newworkordernodeInfo.containsKey("nodeName")) {
			String nodeName = newworkordernodeInfo.getString("nodeName");
			if (CommonUtil.isSpecialChar(nodeName)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "新建节点不能出现特殊字符");
			}
		}
		if (CollectionUtils.isNotEmpty(workordernodeInfo)) {
			for (int i = 0; i < workordernodeInfo.size(); i++) {
				String nodeName = workordernodeInfo.getJSONObject(i).getString("nodeName");
				if (CommonUtil.isSpecialChar(nodeName)) {
					return new DbResponse(ResponseConstant.RES_FAIL_CODE, "流转节点不嫩出现特殊字符");
				}
			}
		}
		Map<String, Object> searchMap = new HashMap<>(8);
		searchMap.put("name", workOrderTypeName);
		searchMap.put("useStatus", ConstantCode.STATUS_NOMAL_OLD);
		searchMap.put("tenantId", tenantId);
		List<WorkOrderTypeVo> workOrderTypeVoList = workOrderTypeService.getWorkOrderTypeListByCondition(searchMap);
		if (workOrderTypeVoList != null && !workOrderTypeVoList.isEmpty()) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, workOrderTypeName + "已存在");
		}
		WorkOrderTypeVo workOrderTypeVo = workOrderTypeDao.selectByTypeName(workOrderTypeName,tenantId);
		if (!Objects.isNull(workOrderTypeVo)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"工单类型名已存在");
		}
		// 校验新增内容参数
		DbResponse checkResult = checkParams(jsonObject);
		if (checkResult != null) {
			return checkResult;
		}

		jsonObject.put("tenantId", tenantId);
		return workOrderTypeService.create(jsonObject);
	}

	@ApiOperation(value = "获取所有的工单类型名称", notes = "获取所有的工单类型名称")
	@RequestMapping(method = RequestMethod.GET, value = "/selectAllName")
	public DbResponse selectAllName(HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		List<WorkOrderTypeVo> workOrderTypeVoList = workOrderTypeService.selectAllName(tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(workOrderTypeVoList);
		return dbResponse;
	}

	@ApiOperation(value = "获取所有可选择的工单类型(包含全部类型)", notes = "获取所有可选择的工单类型(包含全部类型)")
	@RequestMapping(method = RequestMethod.GET, value = "/selectAllSelectable")
	public DbResponse selectAllSelectable(HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		List<InteractiveDataVo> interactiveDataList = workOrderTypeService.selectAllSelectable(tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(interactiveDataList);
		return dbResponse;
	}

	@ApiOperation(value = "根据用户id获取工单类型", notes = "根据用户id获取工单类型")
	@RequestMapping(method = RequestMethod.GET, value = "/getWorkOrderTypeByUserId")
	public DbResponse getWorkOrderTypeByUserId(@RequestParam(name = "userId",
			required = true) String userId, HttpServletRequest request) {
		userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		Map<String, Object> searchMap = new HashMap<>(8);
		if (!ConstantCode.UserRoleDeptType.ALL.equals(userId)) {
			// 当用户id不为all时，再用用户id过滤工单类型
			searchMap.put("userId", userId);
			searchMap.put("useStatus", ConstantCode.STATUS_NOMAL_OLD);
		}

		searchMap.put("tenantId", tenantId);
		List<WorkOrderTypeVo> workOrderTypeVoList =
				workOrderTypeService.getWorkOrderTypeListByCondition(searchMap);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG, workOrderTypeVoList);
	}

	@ApiOperation(value = "根据工单类型名称获取工单类型", notes = "根据工单类型名称获取工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/selectByName")
	public DbResponse selectByName(@ApiParam(value = "selectByName", required = true) @RequestBody SelectByNameAndTypeDto selectByNameAndTypeDto,
								   HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		String name = selectByNameAndTypeDto.getName();
		String userId = selectByNameAndTypeDto.getUserId();
		Integer currentPage = selectByNameAndTypeDto.getCurrentPage();
		Integer pageSize = selectByNameAndTypeDto.getPageSize();
		DbResponse dbResponse = new DbResponse();
		PageOutputDto pageOutputDto = workOrderTypeService.selectByName(name, currentPage, pageSize, userId, tenantId);
		if (pageOutputDto.getList().size() == 0) {
			pageOutputDto = workOrderTypeService.selectByName(name, 1, pageSize, userId, tenantId);
		}
		dbResponse.setRetCode(200);
		dbResponse.setData(pageOutputDto);
		return dbResponse;
	}

	@ApiOperation(value = "更新工单类型的使用状态", notes = "更新工单类型的使用状态")
	@RequestMapping(method = RequestMethod.GET, value = "/updateUseStatus")
	public DbResponse updateUseStatus(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Long id,
									  @ApiParam(value = "userId", required = true) @RequestParam(name = "userId") String userId,
									  @ApiParam(value = "useStatus", required = true) @RequestParam(name = "useStatus") Integer useStatus
	) {
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "更新失败");
		if (StringUtils.isEmpty(userId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "请选择用户");
		}
		WorkOrderTypeVo workOrderTypeVo = workOrderTypeService.selectById(id);
		if (workOrderTypeVo == null) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("该工单类型不存在");
			return dbResponse;
		}
		int retCode = workOrderTypeService.updateUseStatus(id, userId, useStatus);
		if (retCode == 1) {
			dbResponse.setRetCode(200);
			dbResponse.setMsg("更新成功");
		}
		return dbResponse;
	}

	@ApiOperation(value = "根据id获取工单类型的属性内容", notes = "根据id获取工单类型的属性内容")
	@RequestMapping(method = RequestMethod.GET, value = "/selectContentById")
	public DbResponse selectContentById(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Long id,
										HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		WorkOrderTypeVo workOrderTypeVo = workOrderTypeService.selectById(id);
		if (workOrderTypeVo == null) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("该工单类型不存在");
			return dbResponse;
		}
		JSONObject content = workOrderTypeService.selectContentById(id,tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(content);
		return dbResponse;
	}

	@ApiOperation(value = "判断工单类型是否可以编辑", notes = "判断工单类型是否可以编辑")
	@RequestMapping(method = RequestMethod.GET, value = "/judgeEditableById")
	public DbResponse judgeEditableById(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Long id) {
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "更新失败");
		WorkOrderTypeVo workOrderTypeVo = workOrderTypeService.selectById(id);
		if (workOrderTypeVo == null) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("该工单类型不存在");
			return dbResponse;
		}
		dbResponse.setRetCode(200);
		dbResponse.setMsg(workOrderTypeVo.getName() + "业务类型可以编辑");
		return dbResponse;
	}

	@ApiOperation(value = "编辑工单类型的属性", notes = "编辑工单类型的属性")
	@RequestMapping(method = RequestMethod.POST, value = "/updateContent")
	public DbResponse updateContent(@ApiParam(value = "customField", required = true) @RequestBody JSONObject jsonObject,
									HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		log.info("编辑时前端入参: {}", jsonObject);
		Long id = jsonObject.getLong("previousId");
		JSONObject workordertypeformInfo = jsonObject.getJSONObject("workordertypeformInfo");
		String workOrderTypeName = workordertypeformInfo.getString("workordertypeName");
		WorkOrderTypeVo workOrderTypeVo = workOrderTypeService.selectById(id);
		if (workOrderTypeVo == null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "该工单类型不存在");
		}
		workOrderTypeVo = workOrderTypeDao.selectByTypeName(workOrderTypeName,tenantId);
		if (!Objects.isNull(workOrderTypeVo) && !workOrderTypeVo.getId().equals(id)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"工单类型名已存在，更换后重试");
		}
		// 校验更新内容参数
		DbResponse checkResult = checkParams(jsonObject);
		if (checkResult != null) {
			return checkResult;
		}
		// 重置工单类型节点id
		jsonObject = resetWorkOrderTypeNodeId(jsonObject);
		jsonObject.put("tenantId", tenantId);
		try {
			return workOrderTypeService.updateContent(jsonObject);
		} catch (Exception e) {
			log.info("更新流程异常"+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,""+e);
		}
	}

	/**
	 * TODO 重置工单类型节点id和流转按钮节点id
	 *
	 * @param jsonObject 1
	 * @author: victor_tang
	 * @createtime: 2020/4/23 15:51
	 * @return: com.alibaba.fastjson.JSONObject
	 */
	private JSONObject resetWorkOrderTypeNodeId(JSONObject jsonObject) {
		// 初始节点
		JSONObject startNodeInfo = resetNodeInfoTimes(jsonObject.getJSONObject("newworkordernodeInfo"));
		jsonObject.put("newworkordernodeInfo",startNodeInfo);
		// 流转节点
		JSONArray flowNodeInfoList = jsonObject.getJSONArray("workordernodeInfo");
		if (flowNodeInfoList == null || flowNodeInfoList.size() == 0) {
			return jsonObject;
		}
		for (int i = 0; i < flowNodeInfoList.size(); i++) {
			JSONObject jsonObjectNode = flowNodeInfoList.getJSONObject(i);
//			if (!jsonObjectNode.containsKey("sequenceFlows")){
//				continue;
//			}
			resetNodeInfoTimes(jsonObjectNode);
		}
		jsonObject.put("workordernodeInfo",flowNodeInfoList);
		return jsonObject;
	}

	/**
	 * TODO 重置工单类型节点id
	 *
	 * @param nodeInfo 1
	 * @author: victor_tang
	 * @createtime: 2020/4/23 19:52
	 * @return: void
	 */
	private JSONObject resetNodeInfoTimes(JSONObject nodeInfo) {
		Long tempTimes = 1000000000000L;
		if (nodeInfo != null && nodeInfo.getLong("uuid") != null) {
			nodeInfo.put("uuid", nodeInfo.getLong("uuid") + tempTimes);
			JSONArray flowNodeSequenceFlows = nodeInfo.getJSONArray("sequenceFlows");
			String externalCreateNodeId = nodeInfo.getString("externalCreateNodeId");
			if (CollectionUtils.isEmpty(flowNodeSequenceFlows)){
				Long afterFlowNode = nodeInfo.getLong("afterFlowNode");
				nodeInfo.put("afterFlowNode",afterFlowNode+tempTimes);
			}
			else {
				nodeInfo.put("sequenceFlows", resetNodeSequenceFlows(flowNodeSequenceFlows, tempTimes));
				if (!StringUtils.isEmpty(externalCreateNodeId)) {
					Long aLong = Long.valueOf(externalCreateNodeId);
					aLong +=tempTimes;
					nodeInfo.put("externalCreateNodeId",aLong.toString());
				}
			}
		}
		return nodeInfo;
	}
	/**
	 * TODO 重置节点流转按钮节点id
	 *
	 * @param nodeSequenceFlows 1
	 * @author: victor_tang
	 * @createtime: 2020/4/23 19:43
	 * @return: void
	 */
	private JSONArray resetNodeSequenceFlows(JSONArray nodeSequenceFlows, Long tempTimes) {

		for (int i = 0; i < nodeSequenceFlows.size(); i++) {
			JSONObject nodeSequence = nodeSequenceFlows.getJSONObject(i);
			// 重置源节点id
			long sourceRef = nodeSequence.getLong("sourceRef") + tempTimes;
			nodeSequence.put("sourceRef", sourceRef);
			// 重置目标节点id
			String targetRef = nodeSequence.getString("targetRef");

			String type = nodeSequence.getString("type");
			if (!ConstantCode.WorkOrderProcessButton.END.equals(type)) {
				nodeSequence.put("targetRef",
						targetRef != ""?(Long.valueOf(targetRef) + tempTimes + ""):"");

			}
		}
		return nodeSequenceFlows;
	}

	@ApiOperation(value = "删除工单类型", notes = "删除工单类型")
	@RequestMapping(method = RequestMethod.GET, value = "/delete")
	public DbResponse delete(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Long id,
							 @ApiParam(value = "userId", required = true) @RequestParam(name = "userId") String userId,
							 HttpServletRequest request) {
		userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "删除失败");
		WorkOrderTypeVo workOrderTypeVo = workOrderTypeService.selectById(id);
		if (workOrderTypeVo == null) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("该工单类型不存在");
			return dbResponse;
		}
		//所有该工单类型的工单都处于关闭状态时，才可以删除
		Integer closeStatusWorkOrderCount = workOrderDao.selectTotalNotCloseStatus(id);
		if (closeStatusWorkOrderCount > 0) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg(workOrderTypeVo.getName() + "业务类型下有" + closeStatusWorkOrderCount + "条流转的工单，请关闭后重试");
			return dbResponse;
		}
		Integer retCode = workOrderTypeService.updateDeleteStatus(id, userId);
		if (retCode == 1) {
			dbResponse.setRetCode(200);
			dbResponse.setMsg("删除成功");
		}
		return dbResponse;
	}

	@ApiOperation(value = "批量删除工单类型", notes = "批量删除工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/deleteBatch")
	public DbResponse deleteBatch(@RequestBody WorkOrderTypeInputDto workOrderTypeInputDto) {
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "删除失败");
		List<Long> idList = workOrderTypeInputDto.getIdList();
		if (StringUtils.isEmpty(workOrderTypeInputDto.getUserId())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"用户id不能为空！");
		}
		for (Long id : idList) {
			WorkOrderTypeVo workOrderTypeVo = workOrderTypeService.selectById(id);
			if (workOrderTypeVo == null) {
				dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
				dbResponse.setMsg("该表单模板不存在");
				return dbResponse;
			}
			//所有该工单类型的工单都处于关闭状态时，才可以删除
			Integer closeStatusWorkOrderCount = workOrderDao.selectTotalNotCloseStatus(id);
			if (closeStatusWorkOrderCount > 0) {
				dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
				dbResponse.setMsg(workOrderTypeVo.getName() + "业务类型下有"+closeStatusWorkOrderCount+"条流转的工单，请将关闭后重试");
				return dbResponse;
			}
		}
		return workOrderTypeService.deleteBatch(workOrderTypeInputDto);
	}
	/**
	 * TODO 校验工单类型参数
	 * @author: victor_tang
	 * @createtime: 2020/8/17 17:00
	 * @param jsonObject 1
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse checkParams(JSONObject jsonObject) {
		// 校验工单类型基础信息
		DbResponse  typeBaseInfoCheckResult = checkTypeBaseInfo(jsonObject);
		if (typeBaseInfoCheckResult!=null) {
			return 	typeBaseInfoCheckResult;
		}
		// 保存节点信息
		Map nodeMap = new HashMap(16);
		// 校验初始节点信息
		DbResponse  stratNodeCheckResult = checkStartNodeInfo(jsonObject,nodeMap);
		if (stratNodeCheckResult!=null) {
			return 	stratNodeCheckResult;
		}
		// 校验流转节点信息
		DbResponse  flowNodeCheckResult = checkFlowNodeInfo(jsonObject,nodeMap);
		if (flowNodeCheckResult!=null) {
			return 	flowNodeCheckResult;
		}
		// 检验初始节点和流转节点的流转动作
		DbResponse  flowActionCheckResult = checkFlowAction(jsonObject,nodeMap);
		if (flowActionCheckResult!=null) {
			return 	flowActionCheckResult;
		}
		return null;
	}
	/**
	 * TODO 检验所有节点的流转动作
	 * @author: victor_tang
	 * @createtime: 2020/8/17 17:12
	 * @param jsonObject 1
	 * @param nodeMap 2
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse checkFlowAction(JSONObject jsonObject, Map nodeMap) {
		//检验初始节点流转动作
		JSONObject newworkordernodeInfo = jsonObject.getJSONObject("newworkordernodeInfo");
		JSONArray sequenceFlows = newworkordernodeInfo.getJSONArray("sequenceFlows");

		DbResponse  startNodeflowButtonCheckResult = checkFlowButton(sequenceFlows,nodeMap);
		if (startNodeflowButtonCheckResult!=null) {
			return 	startNodeflowButtonCheckResult;
		}

		//检验流转节点流转动作
		JSONArray workordernodeInfo = jsonObject.getJSONArray("workordernodeInfo");
		for (int x = 0; x < workordernodeInfo.size(); x++) {
			JSONObject workordernodeInfo1 = workordernodeInfo.getJSONObject(x);
			JSONArray sequenceFlows1 = workordernodeInfo1.getJSONArray("sequenceFlows");
			String nodeSign = workordernodeInfo1.getString("nodeSign");
			if (ConstantCode.WorkOrderProcessButton.FLOW_OUT.equals(nodeSign)) {
				continue;
			}
			if (CollectionUtils.isEmpty(sequenceFlows1)) {
				String nodeName = workordernodeInfo1.getString("nodeName");
				return new DbResponse(ResponseConstant.RES_FAIL_CODE,
						nodeName+"节点，流转动作不能为空！");
			}
			DbResponse  flowNodeFlowButtonCheckResult = checkFlowButton(sequenceFlows1,nodeMap);
			if (flowNodeFlowButtonCheckResult!=null) {
				return 	flowNodeFlowButtonCheckResult;
			}
		}
		return null;
	}
	/**
	 * TODO 校验流转按钮
	 * @author: victor_tang
	 * @createtime: 2020/8/17 17:25
	 * @param sequenceFlows 1
	 * @param nodeMap 2
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse checkFlowButton(JSONArray sequenceFlows, Map nodeMap) {
		for (int i = 0; i < sequenceFlows.size(); i++) {
			JSONObject sequenceFlow = sequenceFlows.getJSONObject(i);
			String type = sequenceFlow.getString("type");
			if (StringUtils.isBlank(type)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "处理动作不能为空");
			}
			String name = sequenceFlow.getString("name");
			if (StringUtils.isBlank(name)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "处理动作名字不能为空");
			}
			String targetRef = sequenceFlow.getString("targetRef");
			String status = sequenceFlow.getString("status");
			if (StringUtils.isBlank(status)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "处理后状态不能为空");
			}
			if (ConstantCode.WorkOrderProcessButton.JUMP.equals(type)) {
				if (StringUtils.isBlank(targetRef)) {
					return new DbResponse(ResponseConstant.RES_FAIL_CODE, "目标节点不能为空");
				}
				Object result = nodeMap.get(Long.parseLong(targetRef));
				if (null == result) {
					return new DbResponse(ResponseConstant.RES_FAIL_CODE, "目标节点找不到");
				}
			}
		}
		return null;
	}

	/**
	 * TODO 检验工单类型基础信息
	 * @author: victor_tang
	 * @createtime: 2020/8/17 16:58
	 * @param jsonObject 1
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse checkTypeBaseInfo(JSONObject jsonObject) {
		JSONObject workordertypeformInfo = jsonObject.getJSONObject("workordertypeformInfo");
		String workOrderTypeName = workordertypeformInfo.getString("workordertypeName");
		JSONObject newworkordernodeInfo = jsonObject.getJSONObject("newworkordernodeInfo");
		String previousId = newworkordernodeInfo.getString("previousId");
		if (StringUtils.isBlank(workOrderTypeName)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "工单类型名称不能为空");
		}
		else if (workOrderTypeName.length() > ConstantCode.ControllerStrLengthCheck.LENGTH) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "工单类型名称不能超过80个字");
		}

		return null;
	}

	/**
	 * TODO 校验流转节点信息
	 * @author: victor_tang
	 * @createtime: 2020/8/17 16:55
	 * @param jsonObject 1
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse checkFlowNodeInfo(JSONObject jsonObject,Map nodeMap) {
		//检验流转节点信息
		JSONArray workordernodeInfo = jsonObject.getJSONArray("workordernodeInfo");
		for (int j = 0; j < workordernodeInfo.size(); j++) {
			JSONObject workordernodeInfo1 = workordernodeInfo.getJSONObject(j);
			String workordernodeInfo1Name = workordernodeInfo1.getString("nodeName");
			if (StringUtils.isBlank(workordernodeInfo1Name)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "节点名称不能为空");
			}
			if (workordernodeInfo1Name.length() > ConstantCode.ControllerStrLengthCheck.LENGTH) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "节点名称不能超过80个字");
			}
			Long workordernodeInfo1Id = workordernodeInfo1.getLong("uuid");
			nodeMap.put(workordernodeInfo1Id, workordernodeInfo1Name);
			String newworkordernodeFormId1 = workordernodeInfo1.getString("formKey");
			if (StringUtils.isBlank(newworkordernodeFormId1)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "模板不能为空");
			}
			String nodeSign = workordernodeInfo1.getString("nodeSign");
			if (ConstantCode.WorkOrderProcessButton.FLOW_OUT.equals(nodeSign)) {
				// 外部流转节点校验
				DbResponse outResult = outFlowNodeCheck(workordernodeInfo1);
				if (outResult!=null) {
					return 	outResult;
				}
			}else{
				DbResponse normalResult = normalFlowNodeCheck(workordernodeInfo1);
				// 正常流转节点校验
				if (normalResult!=null) {
					return 	normalResult;
				}
			}
		}
		return null;
	}
	/**
	 * TODO 外部流转节点校验
	 * @author: victor_tang
	 * @createtime: 2020/8/18 15:08
	 * @param workordernodeInfo1 1
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse outFlowNodeCheck(JSONObject workordernodeInfo1) {
		Integer flowInStatus = workordernodeInfo1.getInteger("flowInStatus");
		if (flowInStatus==null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "转入后状态不能为空！");
		}
		String afterFlowNode = workordernodeInfo1.getString("afterFlowNode");
		if (StringUtils.isEmpty(afterFlowNode)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "转入后下一步节点不能为空！");
		}
		JSONObject flowOutApi = workordernodeInfo1.getJSONObject("flowOutApi");
		if (flowOutApi==null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "外部流转转出接口地址，参数不能为空！");
		}
		if (StringUtils.isEmpty(flowOutApi.getString("getAddress"))) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"外部流转转出接口地址不能为空！");
		}
		if (StringUtils.isEmpty(flowOutApi.getString("paramsInfo"))) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "外部流转转出接口参数不能为空！");
		}
		return null;
	}

	/**
	 * TODO 正常流转节点校验
	 * @author: victor_tang
	 * @createtime: 2020/8/18 15:05
	 * @param workordernodeInfo1 1
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse normalFlowNodeCheck(JSONObject workordernodeInfo1) {
		String assigneetype = workordernodeInfo1.getString("assigneetype");
		if (StringUtils.isBlank(assigneetype)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "处理人不能为空");
		}
		List assignee = workordernodeInfo1.getObject("assignee", List.class);
		if (assignee.size() == 0) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "处理人不能为空");
		}
		String dealtime = workordernodeInfo1.getString("dealtime");
		if (StringUtils.isBlank(dealtime)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "处理时长不能为空");
		}
		// 默认设置为查看后状态不变，待此功能设计完善后再做定制化开发
		workordernodeInfo1.put("afterviewstatus", ConstantCode.WorkOrderState.NO_CHANGE);
		String afterviewstatus = workordernodeInfo1.getString("afterviewstatus");
		if (StringUtils.isBlank(afterviewstatus)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "查看后状态不能为空");
		}
		return null;
	}

	/**
	 * TODO 校验初始节点信息
	 * @author: victor_tang
	 * @createtime: 2020/8/17 16:52
	 * @param jsonObject 1
	 * @return: com.egoo.ticket.server.utils.common.DbResponse
	 */
	private DbResponse checkStartNodeInfo(JSONObject jsonObject,Map nodeMap) {
		//校验初始节点节点信息
		JSONObject newworkordernodeInfo = jsonObject.getJSONObject("newworkordernodeInfo");
		Long newworkordernodeId = newworkordernodeInfo.getLong("uuid");
		String newworkordernodeName = newworkordernodeInfo.getString("nodeName");
		if (StringUtils.isBlank(newworkordernodeName)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "节点名称不能为空");
		} else {
			if (newworkordernodeName.length() > ConstantCode.ControllerStrLengthCheck.LENGTH) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "节点名称不能超过80个字");
			}
		}
		nodeMap.put(newworkordernodeId, newworkordernodeName);
		String formKeySign = newworkordernodeInfo.getString("formKeySign");
		if (ConstantCode.WorkOrderNodeInfo.FORM_KEY_SIGN_ONE.equals(formKeySign)) {
			String newworkordernodeFormId = newworkordernodeInfo.getString("formKey");
			if (StringUtils.isBlank(newworkordernodeFormId)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "初始节点模板不能为空");
			}
		}
		else if (ConstantCode.WorkOrderNodeInfo.FORM_KEY_SIGN_MORE.equals(formKeySign)) {
			// 检验初始节点多模板必填参数
			JSONArray classifyList = newworkordernodeInfo.getJSONArray("classifyList");
			if (CollectionUtils.isEmpty(classifyList)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "初始节点问题分类不能为空！");
			}
		}
		// 校验是否外部创建
		boolean isCreateWay = newworkordernodeInfo.getBoolean("createWay");
		if (isCreateWay) {
			String externalCreateNodeId = newworkordernodeInfo.getString("externalCreateNodeId");
			if (StringUtils.isEmpty(externalCreateNodeId)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "外部创建下一步节点不能为空！");
			}
			String externalCreateStatus = newworkordernodeInfo.getString("externalCreateStatus");
			if (StringUtils.isEmpty(externalCreateNodeId)) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "外部创建创建后不能为空！");
			}
		}

		return null;
	}

}
